package com.esun.cheetah.view.odds.activity;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import java.util.Map;

import com.esun.cheetah.R;

import com.esun.cheetah.biz.match.matchlist.FilterUtil;

import com.esun.cheetah.config.Key;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.view.match.matchlist.CalendarActivity;

import com.esun.util.TimeUtil;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.PopupWindow.OnDismissListener;

/**
 * 
 * @Package com.esun.cheetah.view.odds.activity.OddsAgp
 * @ClassName: OddsAgp
 * @Description: TODO(odds activity group)
 * @author handy <br/>
 * @date create at 2012-7-31 下午2:07:17
 * 
 */
public class OddsAgp extends ActivityGroup {

	/**
	 * title LinearLayout
	 */
	private LinearLayout title_layout;
	/**
	 * content LinearLayout
	 */
	private LinearLayout linearContent;
	private ImageView arrow;
	private TextView title;
	private Button filterCompany;
	private Button filterMatch;

	private PopupWindow window;
	private ListView list;
	/** Date format class */
	private static Format formatDay = new SimpleDateFormat("dd");
	private static Format formatDate = new SimpleDateFormat("yyyy.MM");
	private TextView tvDay, tvWeekend, tvDate;
	private RelativeLayout rlDate;

	/**
	 * the flag that pop is open or close
	 */
	private boolean isOpenPop = false;
	ArrayList<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
	Context mContext;

	protected static final int REQUEST_CODE_CALENDAR = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.odds_activity_group);

		/**
		 * The server has recently get time initialization list by use of a
		 * calendar
		 */
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(TimeUtil.getInstance().getLatestRemoteTime());
		// MatchListManager.getInstance().setCalendar(calendar);
		initView();
	}

	private void initView() {

		tvDay = (TextView) findViewById(R.id.tv_match_list_agp_title_day);
		tvWeekend = (TextView) findViewById(R.id.tv_match_list_agp_title_weekend);
		tvDate = (TextView) findViewById(R.id.tv_match_list_agp_title_date);
		rlDate = (RelativeLayout) findViewById(R.id.ll_match_list_agp_title_date_layout);
		rlDate.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(OddsAgp.this, CalendarActivity.class);
				getParent().startActivity(intent);
			}
		});

		FilterUtil.getInstance().setToday(true);
		Date date = TimeUtil.getInstance().getLatestRemoteTime();
		tvDay.setText(formatDay.format(date));
		tvDate.setText(formatDate.format(date));
		tvWeekend.setText("（今天）");
		linearContent = (LinearLayout) findViewById(R.id.oddsgroup_linear_content);

		swichActivitys(0);
		mContext = this;
		arrow = (ImageView) findViewById(R.id.arrow);
		filterCompany = (Button) findViewById(R.id.filtercompany);
		filterCompany.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});

		filterMatch = (Button) findViewById(R.id.filtercompany);
		filterMatch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
		title = (TextView) findViewById(R.id.title);
		title_layout = (LinearLayout) findViewById(R.id.title_layout);
		title_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				changPopState(v);

			}
		});

	}

	/**
	 * change Activity
	 * 
	 * @param position
	 */
	private void swichActivitys(int position) {
		linearContent.removeAllViews();

		switch (position) {
		case 0:

			linearContent.removeAllViews();

			linearContent.addView(getLocalActivityManager().startActivity(
					"yapan", new Intent(this, YaPan.class)).getDecorView());
			Log.e("position--------------", "亚盘");
			break;

		case 1://
			linearContent.removeAllViews();
			linearContent.addView(getLocalActivityManager().startActivity(
					"EurOdds", new Intent(this, EurOddsAct.class))
					.getDecorView());
			Log.e("position--------------", "欧赔");
			break;

		case 2://
			linearContent.removeAllViews();
			linearContent.addView(getLocalActivityManager().startActivity(
					"bigsmallball", new Intent(this, DaXiaQiu.class))
					.getDecorView());
			Log.e("position--------------", "大小");
			break;

		default:
			break;
		}

	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see android.app.ActivityGroup#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		// this.registerReceiver(
		// ConnectionChangeReceiver.getConnectionChangeReceiver(),
		// ConnectionChangeReceiver.getIntentFilter());

	}

	@Override
	protected void onPause() {
		super.onPause();
		// this.unregisterReceiver(ConnectionChangeReceiver
		// .getConnectionChangeReceiver());
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Activity parent = getParent();
			if (parent != null) {
				parent.onKeyDown(keyCode, event);
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * change pop
	 * 
	 * @param v
	 */

	public void changPopState(View v) {

		isOpenPop = !isOpenPop;
		if (isOpenPop) {
			arrow.setBackgroundResource(R.drawable.icon_default_packup);
			popAwindow(v);

		} else {
			arrow.setBackgroundResource(R.drawable.icon_default_unfold);
			if (window != null) {
				window.dismiss();

			}
		}
	}

	/**
	 * set window of pop
	 * 
	 * @param parent
	 */
	private void popAwindow(View parent) {
		if (window == null) {
			LayoutInflater lay = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View v = lay.inflate(R.layout.pop, null);
			list = (ListView) v.findViewById(R.id.pop_list);

			SimpleAdapter adapter = new SimpleAdapter(this, CreateData(),
					R.layout.pop_list_item, new String[] { Key.KEY },
					new int[] { R.id.title });

			list.setAdapter(adapter);
			list.setItemsCanFocus(false);
			list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
			list.setOnItemClickListener(listClickListener);
			// window = new PopupWindow(v, 260, 300);
			int x = (int) getResources().getDimension(R.dimen.pop_x);
			int y = (int) getResources().getDimension(R.dimen.pop_y);
			window = new PopupWindow(v, x, y);
		}
		window.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.bg_default_470x552));
		window.setFocusable(true);
		window.setOutsideTouchable(false);
		window.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				// TODO Auto-generated method stub
				isOpenPop = false;
				arrow.setBackgroundResource(R.drawable.icon_default_unfold);
			}
		});
		window.update();
		window.showAtLocation(parent, Gravity.CENTER_HORIZONTAL | Gravity.TOP,
				0, (int) getResources().getDimension(R.dimen.pop_layout_y));

	}

	OnItemClickListener listClickListener = new OnItemClickListener() {
		@SuppressWarnings("unchecked")
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			Map<String, Object> map = (Map<String, Object>) parent
					.getItemAtPosition(position);

			Toast.makeText(mContext, map.get(Key.KEY) + "", Toast.LENGTH_SHORT)
					.show();

			swichActivitys(position);
			title.setText(map.get(Key.KEY) + "");
			if (window != null) {
				window.dismiss();

			}

		}
	};

	/**
	 * set title content
	 * 
	 * @return items
	 */
	public ArrayList<Map<String, Object>> CreateData() {

		Map<String, Object> map;
		map = new HashMap<String, Object>();
		map.put(Key.KEY, Language.YAPAN);
		items.add(map);
		map = new HashMap<String, Object>();
		map.put(Key.KEY, Language.EURODDS);
		items.add(map);
		map = new HashMap<String, Object>();
		map.put(Key.KEY, Language.DAXIAOQIU);
		items.add(map);

		return items;

	}
}
